#include "Camera.h"
//------------------------------------------------------------------------
// Kon-/Destruktoren
//------------------------------------------------------------------------
Camera::Camera(IrrlichtDevice * const dev)
	: Entity(dev)
{
	this->init();
}
//------------------------------------------------------------------------
void Camera::init()
{
	this->m_camera = this->m_sceneMgr->addCameraSceneNode(nullptr, vector3df(0.0f, 0.0f, -Global::Konstanten::gc_CameraDistance), vector3df(0.0f, 0.0f, 0.0f));
	
	this->m_cameraAngleSpeed = 90.0f;
	this->m_cameraYaw = 0.0f;
	this->m_cameraPitch = 0.0f;
}
//------------------------------------------------------------------------
vector3df Camera::getPosition(void) const
{
	return this->m_camera->getPosition();
}
//------------------------------------------------------------------------
vector3df Camera::getLookVec(void) const
{
	return this->m_camera->getTarget();
}
//------------------------------------------------------------------------
vector3df Camera::getUpVec(void) const
{
	return this->m_camera->getUpVector();
}
//------------------------------------------------------------------------
void Camera::pitchUp(float timefactor)
{

	this->m_cameraPitch += timefactor * this->m_cameraAngleSpeed;

	this->m_cameraPitch = std::min(this->m_cameraPitch, 89.9f);

	this->placeCamera();
}
//------------------------------------------------------------------------
void Camera::pitchDown(float timefactor)
{
	this->m_cameraPitch += timefactor * -this->m_cameraAngleSpeed;

	this->m_cameraPitch = std::max(this->m_cameraPitch, 0.0f);

	this->placeCamera();
}
//------------------------------------------------------------------------
void Camera::turnLeft(float timefactor)
{
	this->m_cameraYaw += timefactor * -this->m_cameraAngleSpeed;
	this->m_cameraYaw = std::fmod(this->m_cameraYaw+360.f, 360.0f);

	this->placeCamera();
}
//------------------------------------------------------------------------
void Camera::turnRight(float timefactor)
{
	this->m_cameraYaw += timefactor * this->m_cameraAngleSpeed;
	this->m_cameraYaw = std::fmod(this->m_cameraYaw+360.0f, 360.0f);

	this->placeCamera();
}
//------------------------------------------------------------------------
void Camera::placeCamera()
{
	vector3df camRotation(0.0f, 0.0f, -1.0f); //= this->m_camera->getPosition();

	camRotation.rotateYZBy(this->m_cameraPitch);
	camRotation.rotateXZBy(this->m_cameraYaw);

	this->m_camera->setPosition(camRotation*Global::Konstanten::gc_CameraDistance);

/*#ifdef _DEBUG
	std::cout << "CameraVector: " << vector3df(this->m_camera->getPosition()).normalize();
	std::cout << " Pitch/Yaw: " << this->m_cameraPitch << " | " << this->m_cameraYaw << std::endl;
#endif*/
}
//------------------------------------------------------------------------
